<template>
  <div>
    <!-- 面包屑组件 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <!-- 需要用native属性 -->
      <el-breadcrumb-item :to="{ path: '/home' }" @click.native="returnHome"
        >首页</el-breadcrumb-item
      >
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图 -->
    <el-card>
      <!-- 搜素框  根据query参数重新请求数据，并带有一个清空的x，并绑定事件-->
      <el-row :gutter="10">
        <el-col :span="8"
          ><el-input
            placeholder="请输入内容"
            clearable
            v-model="queryInfo.query"
            closable
            @clear="getGoodsList"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getGoodsList"
            ></el-button> </el-input
        ></el-col>
        <el-col :span="4">
          <el-button type="primary" @click="addGood">添加商品</el-button>
        </el-col>
      </el-row>

      <!-- table商品详细展示区 -->
      <el-table stripe border :data="goodsList">
        <el-table-column type="index"></el-table-column>
        <el-table-column label="商品名称" prop="goods_name"></el-table-column>
        <el-table-column
          width="105px"
          label="商品价格(元)"
          prop="goods_price"
        ></el-table-column>
        <el-table-column
          label="商品重量"
          prop="goods_weight"
          width="90px"
        ></el-table-column>
        <!-- 时间戳转为时间格式 -->
        <el-table-column label="创建时间" prop="add_time" width="180px">
          <!-- 使用全局过滤器来调用时间格式的转换 -->
          <template v-slot="{ row }">
            {{ row.add_time | dateFormat }}
          </template></el-table-column
        >
        <el-table-column label="操作" width="130px">
          <template v-slot="{ row }">
            <!-- 作用域插槽，row可以获得当前行的所有数据 -->
            <!-- 修改按钮 -->
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="showEditDialog(row)"
            ></el-button>
            <!-- 删除按钮 -->
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="deleteGoodById(row.goods_id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页区 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[5, 10, 15]"
        :page-size="10"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>

    <!-- 编辑会话框 -->
    <el-dialog title="提示" :visible.sync="editDialogVisible" width="50%">
      <el-form
        :model="editRuleForm"
        :rules="editRules"
        ref="editRuleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="商品名称" prop="goods_name">
          <el-input v-model="editRuleForm.goods_name"> </el-input>
        </el-form-item>
        <el-form-item label="商品价格" prop="goods_price">
          <el-input v-model="editRuleForm.goods_price">
          </el-input> </el-form-item
        ><el-form-item label="商品重量" prop="goods_weight">
          <el-input v-model="editRuleForm.goods_weight"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editGood">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'List',
  data () {
    return {
      // 查询数据参数
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 10
      },
      goodsList: [],
      total: 0,
      // 编辑会话框数据
      editDialogVisible: false,
      editRuleForm: {
        goods_name: '',
        goods_price: 0,
        goods_number: 0,
        goods_weight: 0
      },
      goods_id: 0,
      editRules: {}
    }
  },
  methods: {
    returnHome () {
      this.$bus.$emit('changePath')
    },
    async getGoodsList () {
      const { data: res } = await this.$https.get('goods', { params: this.queryInfo })
      if (res.meta.status !== 200) {
        return this.$message.error('获取商品列表信息失败！')
      }
      this.total = res.data.total
      this.goodsList = res.data.goods
    },
    // 分页区方法
    handleSizeChange (newSize) {
      this.queryInfo.pagesize = newSize
      this.getGoodsList()
    },
    handleCurrentChange (newPage) {
      this.queryInfo.pagenum = newPage
      this.getGoodsList()
    },
    // 删除商品
    async deleteGoodById (id) {
      // 返回的是一个promise对象 确定返回confirm 否则返回cancel字符串
      const result = await this.$confirm('此操作将永久删除该分类, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(error => error)
      // 发送删除请求 并刷新列表
      if (result === 'confirm') {
        const { data: res } = await this.$https.delete(`goods/${id}`)
        if (res.meta.status === 200) {
          this.$message.success('删除成功！')
          this.getGoodsList()
        } else this.$message.error('删除失败！')
        console.log(res)
      }
    },
    // 编辑
    showEditDialog (good) {
      this.editDialogVisible = true
      // 绑定数据
      this.editRuleForm.goods_price = good.goods_price
      this.editRuleForm.goods_name = good.goods_name
      this.editRuleForm.goods_number = good.goods_number
      this.editRuleForm.goods_weight = good.goods_weight
      this.goods_id = good.goods_id
    },
    async editGood () {
      console.log(this.editRuleForm)
      const { data: res } = await this.$https.put('goods/' + this.goods_id, this.editRuleForm
      )
      if (res.meta.status !== 201) {
        this.$message.error('修改失败！')
      } else {
        this.getGoodsList()
      }
      console.log(res)
      this.editDialogVisible = false
    },
    // 路由跳转
    addGood () {
      this.$router.push('/goods/addgood')
    }
  },
  created () {
    this.getGoodsList()
  }
}
</script>

<style scoped>
</style>
